//
//  FlashStarView.m
//  ImageAnimation
//
//  Created by apple on 8/29/11.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "FlashStarView.h"

@implementation FlashStarView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        [NSTimer scheduledTimerWithTimeInterval:0.3 target:self selector:@selector(changeAlpha) userInfo:nil repeats:YES];
        UIImage *image = [UIImage imageNamed:@"star.jpg"];
        UIImageView *imageView = [[UIImageView alloc] initWithImage:image];
        [imageView setFrame:self.bounds];
        [self addSubview:imageView];
        UIButton *button = [[UIButton alloc]initWithFrame:self.bounds];

        [button setBackgroundColor:[UIColor clearColor]];
        [button addTarget:self action:@selector(clickStar) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
    }
    return self;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void)changeAlpha{
    [UIView beginAnimations:@"changeAlpha" context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3];
    if (self.alpha == 0.2f) {
        [self setAlpha:1];
    }else{
        [self setAlpha:0.2f];
    }
    
//    if ([self.backgroundColor isEqual:[UIColor clearColor]]) {
//        [self setBackgroundColor:[UIColor redColor]];
//    }else{
//        [self setBackgroundColor:[UIColor clearColor]];
//    }
    [UIView commitAnimations];
}

- (void)clickStar{
    
    UIViewController *textViewController = [[UIViewController alloc]init];
    UITextView *text = [[UITextView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    [text setText:@" Only override drawRect: if you perform custom drawing.\nAn empty implementation adversely affects performance during animation."];
    
    [textViewController.view addSubview:text];
    [textViewController.view setFrame:text.bounds];
    popoverController = [[UIPopoverController alloc] initWithContentViewController:textViewController];
    [popoverController setPopoverContentSize:CGSizeMake(200,200)];
    CGRect rect = self.frame;
    //rect.size.height = 0;
    [popoverController presentPopoverFromRect:rect inView:self.superview permittedArrowDirections:UIPopoverArrowDirectionDown animated:NO];
}

@end










